//app.js
var qcloud = require('./vendor/wafer2-client-sdk/index')
var config = require('./config')
var util = require('utils/util.js')
App({
  data:{
    userInfo: {},
    userDetail:{},
    userHistory:[],
    tempItem:[],
    logged: false,
    takeSession: false,
    requestResult: '',
    rank:['N5弱鸡','N4渣渣','N3菜逼','N2入门','N1精通','N0巨佬'],
    serverUrl:'https://14ch.cn:7998/api/'
  },
  //获取用户详细信息
  getUserInfo(data){
    if(this.data.serverUrl){
      //已部署服务器
      wx.request({
        url:`${this.data.serverUrl}getUserInfo`,
        method:'post',
        data:data,
        success: res=>{
          console.log(res);
          if(!res.data.error){
            this.data.userDetail = res.data.data;
            this.setUserInfo({
              UUID: data.UUID,
              nickName: this.data.userInfo.nickName,
              picUrl: this.data.userInfo.avatarUrl
            })
            return res.data.data;
          } else if (res.data.error === "Error: User not found"){
            this.data.userDetail = config.data.userDetail;
            this.setUserInfo({
              UUID:data.UUID,
              classId:null,
              expSingle:0,
              expTwin:0,
              nickName:this.data.userInfo.nickName,
              picUrl:this.data.userInfo.avatarUrl
            })
            return config.data.userDetail;
          }else{
            wx.showToast({
              title: res.data.error,
            })
          }
        }
      })
    }else{
      //未部署服务器
      this.data.userDetail = config.data.userDetail;
     
      return this.data.userDetail;
    } 
  },
  //获取用户历史
  getUserHistory(data,callback){
    if(this.data.serverUrl){
      //已部署服务器
      wx.request({
        url: `${this.data.serverUrl}getUserHistory`,
        data:data,
        method:'post',
        success:res=>{
          if (!res.data.error) {
            callback(res.data.data);
          } else {
            wx.showToast({
              title: res.data.err,
            })
          }
        }
      })
    }else{
      this.data.userHistory=config.data.userHistory
    }
  },
  //获取题目
  getItem(data,callback){
    if(this.data.serverUrl){
      //已部署服务器
      wx.request({
        url: `${this.data.serverUrl}getItem`,
        data: data,
        method:'post',
        success: res => {
            console.log(res);
            this.data.tempItem = res.data.data;
            if(callback) callback(res.data.data);
        }
      })
    }else{
      return config.data.item
    }
  },
  getClassList(){
    if (this.data.serverUrl) {
      //已部署服务器
      wx.request({
        url: `${this.data.serverUrl}getItem`,
        data: data,
        success: res => {
          if (!res.data.error) {
            return res.data.data;
          } else {
            wx.showToast({
              title: res.data.err,
            })
          }
        }
      })
    } else {
      return config.data.classList
    }
  },
  setUserInfo(data){
    wx.request({
      url:`${this.data.serverUrl}setUserInfo`,
      data:data,
      method:'post',
      success:res=>{
        console.log(res);
        if(res.data.error){
          wx.showToast({
            title: res.data.error,
            icon: "none"
          })
        }
        
      }
    })
  },
  addHistory(data){
    wx.request({
      url:`${this.data.serverUrl}addHistory`,
      data:data,
      method:'post',
      success:res=>{
        console.log(res);
      }
    })
  }
  ,
  loginFail(){
    console.log('登陆信息不存在');
    const that=this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          qcloud.login({
            success(result) {

              if (result) {
                that.getUserInfo({ UUID: result.openId });
                util.showSuccess('登录成功');
                that.data.userInfo = result;
                that.data.logged = true;
                wx.setStorage({
                  key: 'userInfo',
                  data: result,
                })
              } else {
                // 如果不是首次登录，不会返回用户信息，请求用户信息接口获取
                console.log(2)
                qcloud.request({
                  url: config.service.requestUrl,
                  login: true,
                  success(result) {
                    that.getUserInfo({ UUID: res.data.data.openId });
                    that.data.userInfo = result.data.data;
                    that.data.logged = true;
                    wx.setStorage({
                      key: 'userInfo',
                      data: result.data.data,
                    })
                  },

                  fail(error) {
                    util.showModel('请求失败', error)
                    console.log('request fail', error)
                  }
                })
              }
            },

            fail(error) {

              //  util.showModel('登录失败', error)
              console.log('登录失败', error)
              if (error.type === 'ERR_WX_GET_USER_INFO') {
                wx.redirectTo({
                  url: '../login/login',
                })
              }
            }
          })
        } else {
          wx.redirectTo({
            url: '../login/login',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      }
    });
  },
      onLaunch: function () {
      
       qcloud.setLoginUrl(config.service.loginUrl);
       util.showBusy('正在连接')
       var that = this
       wx.getStorage({
         key: 'userInfo',
         success: function(res) {
          if(!res.data.openId){
            that.loginFail();
          }else{
            that.data.userInfo=res.data;
            that.data.logged=true;
            that.getUserInfo({ UUID: res.data.openId });
          }
         },
         fail:function(){
            that.loginFail();
         }
       })
      
      
    }
       
})